﻿function oneItemClick(obj, key) {
    if (key == 0) {
        $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) { //i 是1、2、3
            $(this).removeClass("active");
            fileCheck(this, false);
        });
        $(obj).addClass("active");
        fileCheck(obj, true);
    }
    if (key == 1) {
        if ($(obj).hasClass("active")) {
            $(obj).removeClass("active");
            fileCheck(obj, false);
        } else {
            $(obj).addClass("active");
            fileCheck(obj, true);
        }
    }
    if (key == 2) {
        var currentIndex = parseInt($(obj).attr("cid"));
        var min = 0, max = 0;
        $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) { //i 是1、2、3
            if ($(this).hasClass("active")) {
                var index = parseInt($(this).attr("cid"));
                if (min == 0) {
                    min = index;
                } else {
                    if (index > max) {
                        max = index;
                    }
                }
            }
        });
        if (min == 0) {
            $(obj).addClass("active");
            fileCheck(obj, true);

        }
        if (min != 0 && max == 0 && min < currentIndex) {
            fileCheckByshift(min, currentIndex);
        }
        if (min != 0 && max == 0 && min > currentIndex) {
            fileCheckByshift(currentIndex, min);
        }
        if (min != 0 && max == 0 && min == currentIndex) { }


        if (min != 0 && max != 0 && currentIndex <= min) {
            fileCheckByshift(currentIndex, max);
        }
        if (min != 0 && max != 0 && currentIndex >= max) {
            fileCheckByshift(min, currentIndex);
        }
        if (min != 0 && max != 0 && currentIndex > min && currentIndex < max) {
            fileCheckByshift(min, currentIndex);
        }
    }
    buttonDisplay();
}

function myCheckboxClick(obj, key) {
    if (key == 0) {
        if ($(obj)[0].checked == false) {
            $(obj)[0].checked = true;
            $(obj).parent().parent().addClass("active");
        } else {
            $(obj)[0].checked = false;
            $(obj).parent().parent().removeClass("active");
        }
    }
    if (key == 2) {
        var currentIndex = parseInt($(obj).parent().parent().attr("cid"));
        var min = 0, max = 0;
        $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) { //i 是1、2、3
            if ($(this).hasClass("active")) {
                var index = parseInt($(this).attr("cid"));
                if (min == 0) {
                    min = index;
                } else {
                    if (index > max) {
                        max = index;
                    }
                }
            }
        });
        if (min == 0) {
            if ($(obj)[0].checked == false) {
                $(obj)[0].checked = true;
                $(obj).parent().parent().addClass("active");
            } else {
                $(obj)[0].checked = false;
                $(obj).parent().parent().removeClass("active");
            }

        }
        if (min != 0 && max == 0 && min < currentIndex) {
            fileCheckByshift(min, currentIndex);
        }
        if (min != 0 && max == 0 && min > currentIndex) {
            fileCheckByshift(currentIndex, min);
        }
        if (min != 0 && max == 0 && min == currentIndex) { }


        if (min != 0 && max != 0 && currentIndex <= min) {
            fileCheckByshift(currentIndex, max);
        }
        if (min != 0 && max != 0 && currentIndex >= max) {
            fileCheckByshift(min, currentIndex);
        }
        if (min != 0 && max != 0 && currentIndex > min && currentIndex < max) {
            fileCheckByshift(min, currentIndex);
        }
    }

    $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) { //i 是1、2、3
        var myCheck = $(this).find(".inp-td label input")[0];
        if (myCheck != null && myCheck.checked == false) {
            $(this).removeClass("active");
        }
        if (myCheck != null && myCheck.checked) {
            $(this).addClass("active");
        }
    });

    buttonDisplay();

}

function searchFiles(controllerName) {
    var key = $('#fontawsome-search').val();
    $.ajax({
        url: '/' + controllerName + '/SearchAjax?t=' + new Date().getMilliseconds(),
        data: { key: key },
        success: function (data) {
            $('#mybody').html(data);
        },
        error: function (data) {
            console.log("searchFiles()", error.responseText);
        }
    });
}

function commonRenameFolder(controllerName) {
    var lis = $("#myFilesAllItem .myFilesOneItem.active");
    if (lis.length <= 0) {
        showmessage("请先选择文件");
        return;
    }
    if (lis.length > 1) {
        showmessage("只能选择一个文件");
        return;
    }
    if (lis.length == 1) {
        $.ajax({
            url: '/' + controllerName + '/FolderEidtAjaxPage?t=' + new Date().getMilliseconds(),
            data: { add: "rename", newfolderId: lis[0].id },
            success: function (data) {
                $('#mybody').html(data);
            },
            error: function (data) {
                console.log("commonRenameFolder()", error.responseText);
            }
        });
    }
}

function commonSelectFiles(controllerName, actionName) {
    var selectedId = getSelectFiles();
    if (selectedId == "") {
        showmessage("请先选择文件夹或文件");
        return;
    } else {
        $.ajax({
            type: 'post',
            url: '/' + controllerName + '/' + actionName + '?t=' + new Date().getMilliseconds(),
            data: { selectedId: selectedId },
            success: function (data) {
                $('#fileUploadPart').html(data);
            },
            error: function (data) {
                console.log("commonSelectFiles()", error.responseText);
            }
        });
    }
}




function commonDeleteFiles() {
    var selectedId = getSelectFiles();
    if (selectedId == "") {
        showmessage("请先选择团队&群组或文件");
        return;
    } else {
        if (confirm("确定删除团队&群组或文件？\r\n删除后的团队&群组或文件可在回收站内还原")) {
            $.ajax({
                type: 'post',
                url: '/ProjectFiles/DeleteAjax?t=' + new Date().getMilliseconds(),
                data: { selectedId: selectedId },
                success: function (data) {
                    $('#mybody').html(data);
                    showmessage("操作成功");
                    refreshTree("@ViewBag.FolderId");
                },
                error: function (data) {
                    console.log("commonDeleteFiles()", error.responseText);
                }
            });
        } else {
            return;
        }
    }
}

function comDeleteFiles(controllerName) {
    var selectedId = getSelectFiles();
    if (selectedId == "") {
        showmessage("请先选择文件");
        return;
    } else {
        if (confirm("确定删除文件？")) {
            $.ajax({
                type: 'post',
                url: '/' + controllerName + '/DeleteAjax?t=' + new Date().getMilliseconds(),
                data: { selectedId: selectedId },
                success: function (data) {
                    $('#mybody').html(data);
                    showmessage("操作成功");
                },
                error: function (data) {
                    console.log("comDeleteFiles()", error.responseText);
                }
            });
        } else {
            return;
        }
    }
}


function trOnmouseover(obj) {
    c = obj.style.backgroundColor; obj.style.backgroundColor = '#D1E3F5';
    if (location.href.indexOf("Recycle") == -1) {//当不是回收站的时候显示下载按钮，如果是回收站则不显示下载按钮
        $(obj).find(".doc-name a").show();
    }
}
function trOnmouseout(obj) {
    obj.style.backgroundColor = c;
    $(obj).find(".doc-name a").hide();
}

function getSelectFiles() {
    var selectedId = "";
    var lis = $("#myFilesAllItem .myFilesOneItem");
    for (var i = 0; i < lis.length; i++) {
        if (lis[i] != null && $(lis[i]).hasClass("active")) {
            selectedId += lis[i].id + "|";
        }
    }
    return selectedId;
}

function showmessage(text) {
    if ($('#top_alert span').text().length > 0) {
        $('#top_alert').empty().append('<span>' + text + '</span>');
        $('#top_alert').css('display', 'block');
    } else {
        $('.main-container').prepend('<div id="top_alert"><span>' + text + '</span></div>');
    }
    $('#top_alert').fadeOut(5000);
}


function showmessageInLayer(text) {
    if ($('#top_alertInLayer span').text().length > 0) {
        $('#top_alertInLayer').empty().append('<span>' + text + '</span>');
        $('#top_alertInLayer').css('display', 'block');
    } else {
        $('.tips-title').prepend('<div id="top_alertInLayer"><span>' + text + '</span></div>');
    }
    $('#top_alertInLayer').fadeOut(6000);
}
//查看选中的是不是一个文件，选多个或目录则返回false
function getOneFile() {
    var bool = false;
    var selectNum = getSelectNum();
    if (selectNum == 1) {
        $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) {
            if ($(this).hasClass("active")) {
                var type = parseInt($(this).attr("type"));
                if (type == 2) {
                    bool = true;
                }
            }
        });
    }
    return bool;
}

//查看选中的是不是文件，选了文件夹false
function isOnlyFiles() {
    var bool = true;
    $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) {
        if ($(this).hasClass("active")) {
            var type = parseInt($(this).attr("type"));
            if (type == 1) {
                bool = false;
            }
        }
    });

    return bool;
}

//得到当前页面选中的个数
function getSelectNum() {
    var selectNum = 0;
    $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) {
        if ($(this).hasClass("active")) {
            selectNum++;
        }
    });
    return selectNum;
}

function buttonDisplay() {
    var selectNum = getSelectNum();
    //alert(selectNum);
    if (selectNum == 0) {
        setDisplayByName("buttonType1", "none");
        setDisplayById("btnGroup", "none");
        setDisplayById("btnRename", "none");
        setDisplayByName("buttonType2", "none");
    }
    else if (selectNum == 1) {
        setDisplayByName("buttonType1", "");
        setDisplayById("btnGroup", "");
        setDisplayByName("buttonType2", "none");
        setDisplayById("btnRename", "");
        $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) {
            if ($(this).hasClass("active")) {
                var type = parseInt($(this).attr("type"));
                if (type == 1) {
                    setDisplayById("btnDownLoad", "none");
                    //setDisplayById("btnDel", "block");
                    //setDisplayById("btnCollect", "block");

                    setDisplayById("btnViewLog", "none");
                    setDisplayById("btnVersion", "none");
                    setDisplayById("btnDetail", "none");
                }
                else {
                    setDisplayByName("buttonType2", "");
                    setDisplayByName("buttonType1", "");

                }
            }
        });
    }
    else if (selectNum > 1) {
        setDisplayById("btnRename", "none");
        setDisplayByName("buttonType1", "");
        setDisplayById("btnGroup", "");
        setDisplayByName("buttonType2", "none");
    }
    var haveFiles = selectFilesHaveFiles();
    if (haveFiles) {
        setDisplayById("btnAuthorization", "none");
    } else {
        setDisplayById("btnAuthorization", "");
    }
    if (!isOnlyFiles()) {
        setDisplayById("btnCopy", "none");
    }
    else {
        setDisplayById("btnCopy", "");
    }

    var btngroupDisplay = false;
    $("#btnGroup").find(".dropdown-menu a").each(function (i, e) {
        if ($(this)[0].style.display == "" || $(this)[0].style.display == "block") {
            btngroupDisplay = true;
        }
    });
    if (!btngroupDisplay) {
        setDisplayById("btnGroup", "none");
    }
}

function isImage(filetype) {
    if (filetype == ".jpg" || filetype == ".ico" || filetype == ".png" ||
        filetype == ".jpeg" || filetype == ".gif" || filetype == ".bmp") {
        return true;
    }
    return false;
}
function isOffice(filetype) {
    if (filetype == ".doc" || filetype == ".docx" || filetype == ".xls" ||
        filetype == ".xlsx" || filetype == ".ppt" || filetype == ".pptx" ||
        filetype == ".pdf" || filetype == ".potx" || filetype == ".dotm" || filetype == ".ppsx") {
        return true;
    }
    return false;
}

function setDisplayByName(btnName, displayV) {
    var obj = document.getElementsByName(btnName);
    for (var i = 0; i < obj.length; i++) {
        obj[i].style.display = displayV;
    }
}

//初始化按钮选中状态
function initBtn() {
    var selectNum = 0;
    $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) {
        if ($(this).hasClass("active")) {
            $(this).removeClass("active");
        }
    });
}

function setDisplayById(btnId, displayV) {
    var obj = document.getElementById(btnId);
    if (obj != null) { obj.style.display = displayV; }
}
function fileCheckByshift(min, max) {
    $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) { //i 是1、2、3

        $(this).removeClass("active");
        fileCheck(this, false);
    });
    for (var k = min ; k <= max; k++) {
        fileCheckByshiftOne(k, true);
    }
}
function fileCheckByshiftOne(index, filechecked) {
    $("#myFilesAllItem .myFilesOneItem[cid = '" + index + "']").addClass("active");
    var myCheck = $("#myFilesAllItem .myFilesOneItem[cid = '" + index + "'] .inp-td label input")[0];
    if (myCheck != null) {
        myCheck.checked = filechecked;
    }
}
function fileCheck(obj, filechecked) {
    var myCheck = $(obj).find(".inp-td label input")[0];
    if (myCheck != null) {
        myCheck.checked = filechecked;
    }
}

function allSelectCheckbox(obj) {
    $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) { //i 是1、2、3
        $(this).removeClass("active");
        if (obj.checked) {
            $(this).addClass("active");
        }
        fileCheck(this, obj.checked);
    });
    buttonDisplay();
}


//function allSelectCheckbox(obj) {
//    $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) { //i 是1、2、3
//        var myCheck = $(this).find(".inp-td label input")[0];
//        $(this).removeClass("active");
//        if (obj.checked) {
//            $(this).addClass("active");
//        }
//        if (myCheck != null) {
//            myCheck.checked = obj.checked;
//        }
//    });
//    buttonDisplay();
//}

function selectFilesHaveFolder() {
    var lis = $("#myFilesAllItem .myFilesOneItem");
    for (var i = 0; i < lis.length; i++) {
        if (lis[i] != null && $(lis[i]).hasClass("active")) {
            var type = parseInt($(lis[i]).attr("type"));
            if (type == 1) {
                return true;
            }
        }
    }
    return false;
}


function selectFilesHaveFiles() {
    var lis = $("#myFilesAllItem .myFilesOneItem");
    for (var i = 0; i < lis.length; i++) {
        if (lis[i] != null && $(lis[i]).hasClass("active")) {
            var type = parseInt($(lis[i]).attr("type"));
            if (type == 2) {
                return true;
            }
        }
    }
    return false;
}

function copyFiles(type) {

    var selectedId = getSelectFiles();
    if (selectedId == "") {
        showmessage("请先选择文件");
        return;
    } else {
        ////当选中的文件是一个文档时
        //if (type != null && (type.toLowerCase() == "entfiles" || type.toLowerCase() == "projectfiles")) {
        //    if (!isOnlyFiles()) {
        //        showmessage("只可复制文件");
        //        return;
        //    }
        //}
        $.ajax({
            url: '/Common/LoadLayer?t=' + new Date().getMilliseconds(),
            data: { selectedId: selectedId, layerName: '_FileCopyPartial' },
            success: function (data) {
                $('#fileUploadPart').html(data);
            },
            error: function (data) {
                console.log("copyFiles()", error.responseText);
            }
        });
    }
}
function addProject(obj) {
    $.ajax({
        url: '/Common/LoadLayer?t=' + new Date().getMilliseconds(),
        data: { layerName: '_AddProjectPartial', selectedId: obj },
        success: function (data) {
            $('#fileUploadPart').html(data);
        },
        error: function (data) {
            console.log("addProject()", error.responseText);
        }
    });
}



function preView(obj) {
    var selectedId = getSelectFiles();
    if (selectedId == "") {
        showmessage("请先选择文件");
        return;
    } else {
        //  alert(obj);
        window.open("/FilePreview/Index?i=" + obj + "&id=" + selectedId, "_blank");
    }
}

function preViewByActualId(actualId, actualType, actualName) {
    window.open("/FilePreview/Preview?actualId=" + actualId + "&actualType=" + escape(actualType) + "&actualName=" + escape(actualName), "_blank");
}

function preViewByActualIdNoDown(actualId, actualType, actualName) {
    window.open("/FilePreview/PreviewNoDown?actualId=" + actualId + "&actualType=" + escape(actualType) + "&actualName=" + escape(actualName), "_blank");
}

function preView(obj, id) {
    window.open("/FilePreview/Index?i=" + obj + "&id=" + id, "_blank");
}

function preView2(obj, id, couldDownload) {
    window.open("/FilePreview/Index?i=" + obj + "&id=" + id + "&couldDownload=" + couldDownload, "_blank");
}
function preViewProject(id, couldDownload) {
    window.open("/FilePreview/ProIndex?id=" + id + "&couldDownload=" + couldDownload, "_blank");
}


function preView1(obj, id, downForLink) {//downForLink 为true的时候 意思代表  不用登陆也可以下载
    window.open("/FilePreview/Index?i=" + obj + "&id=" + id + "&downForLink=" + downForLink, "_blank");
}

function versionPreView(objectId, actualId, actualName, couldDownload) {
    window.open("/FilePreview/Index1?objectId=" + objectId + "&actualId=" + actualId + "&actualName=" + escape(actualName) + "&couldDownload=" + couldDownload, "_blank");
}

function downFileById(objId, fid, actualName, isCheck) {
    var pageUrl = "/Common/Download?objId=" + objId + "&fid=" + fid + "&actualName=" + escape(actualName) + "&isCheck=" + isCheck;;
    window.open(pageUrl, "_blank");
}


function deleteVersion(versionId, objId, downloadFunction) {
    if (confirm("删除该版本时，该版本的文件会直接删除，在回收站无法恢复，确定删除这个版本吗？")) {
        $.ajax({
            url: '/Common/DeleteVersion?t=' + new Date().getMilliseconds(),
            data: { versionId: versionId, objId: objId, downloadFunction: downloadFunction },
            success: function (data) {
                $('#fileUploadPart').html(data);
                if (location.href.indexOf("MyFiles") > -1) {
                    refreshMyFiles();
                }
                if (location.href.indexOf("EntFiles") > -1) {
                    refreshEntFiles();
                }
                if (location.href.indexOf("ProjectFiles") > -1) {
                    refreshProFiles();
                }
            },
            error: function (data) {
                console.log("deleteVersion()", error.responseText);
            }
        });
    }
}

function refreshMyFiles() {
    $.ajax({
        url: '/MyFiles/AjaxAction?t=' + new Date().getMilliseconds(),
        data: {},
        beforeSend: function () {
            myLoading();
        },
        success: function (data) {
            $('#mybody').html(data);
        },
        error: function (data) {
            console.log("refreshMyFiles()", error.responseText);
        }
    });
}

function refreshEntFiles() {
    $.ajax({
        url: '/EntFiles/AjaxAction?t=' + new Date().getMilliseconds(),
        data: {},
        success: function (data) {
            $('#mybody').html(data);
        },
        error: function (data) {
            console.log("refreshEntFiles()", error.responseText);
        }
    });
}
function refreshProFiles() {
    $.ajax({
        url: '/ProjectFiles/AjaxAction?t=' + new Date().getMilliseconds(),
        data: {},
        success: function (data) {
            $('#mybody').html(data);
        },
        error: function (data) {
            console.log("refreshProFiles()", error.responseText);
        }
    });
}

function viewVersion(deleteFunStr, downloadStr) {
    var download = false;
    if (downloadStr == "true" || downloadStr == "True") {
        download = true;
    }
    var deleteFun = false;
    if (deleteFunStr == "true" || deleteFunStr == "True") {
        deleteFun = true;
    }
    var lis = $("#myFilesAllItem .myFilesOneItem.active");
    if (lis.length <= 0) {
        showmessage("请先选择文件");
        return;
    }
    if (lis.length > 1) {
        showmessage("只能选择一个文件");
        return;
    }
    if (lis.length == 1) {
        $.ajax({
            url: '/Common/VersionView?t=' + new Date().getMilliseconds(),
            data: { selectedId: lis[0].id, deleteFunction: deleteFun, downloadFunction: download },
            success: function (data) {
                $('#fileUploadPart').html(data);
            },
            error: function (data) {
                console.log("viewVersion()", error.responseText);
            }
        });
    }
}



function viewLog(controller) {
    var lis = $("#myFilesAllItem .myFilesOneItem.active");
    if (lis.length <= 0) {
        showmessage("请先选择文件");
        return;
    }
    if (lis.length > 1) {
        showmessage("只能选择一个文件");
        return;
    }
    if (lis.length == 1) {
        $.ajax({
            url: '/' + controller + '/ViewLogShow?t=' + new Date().getMilliseconds(),
            data: { selectedId: lis[0].id },
            success: function (data) {
                $('#fileUploadPart').html(data);
            },
            error: function (data) {
                console.log("viewLog()", error.responseText);
            }
        });
    }
}
function viewDownloadLog(id, controller) {
    $.ajax({
        url: '/' + controller + '/ViewLogShow?t=' + new Date().getMilliseconds(),
        data: { selectedId: id },
        success: function (data) {
            $('#fileUploadPart').html(data);
        },
        error: function (data) {
            console.log("viewDownloadLog()", error.responseText);
        }
    });
}

function entLoading() {
    $('#mybody').html("<div class='mail-body' style='position: fixed; top: 90px; bottom: 0px; right: 0; left: 424px; border-left: 1px solid #efefef;'>" +
        "<div style='width: 100px; margin-top: 200px; margin-left: auto; margin-right: auto;'>" +
        "<img src='/Content/themes/assets/img/loading.gif'>&nbsp;&nbsp; 正在加载 </div></div>");
}

function myLoading() {
    $('#mybody').html("<div class='mail-body' style='position: fixed; top: 128px; bottom: 0px; right: 0; left: 224px; overflow-y: scroll;'>" +
        "<div style='width: 100px; margin-top: 200px; margin-left: auto; margin-right: auto;'>" +
        "<img src='/Content/themes/assets/img/loading.gif'>&nbsp;&nbsp; 正在加载 </div></div>");
}

function loadProjectFiles() {
    window.open('/ProjectFiles', '_self');
    jQuery.support.cors = true;
    $.ajax({
        async: true,
        type: 'post',
        url: 'http://192.168.16.56:8079/api/pro/empty',
        data: {},
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        success: function (data) {
            //  alert(data);
        },
        error: function (data) {
            // alert(data);
            console.log("loadProjectFiles()", error.responseText);
        }
    })
}


function imagePreview(obj) {
    var index = imageIndex(obj);
    var imgs = thisPageImage();
    layer.photos({
        photos: {
            "title": "", //相册标题
            "id": 0, //相册id
            "start": index, //初始显示的图片序号，默认0
            "data": imgs
        }
    });
}
function imageIndex(obj) {
    var index = 0;
    var res = 0;
    $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) { //i 是1、2、3
        var ftype = $(this).attr("ftype");
        if (ftype != null) {
            ftype = ftype.toLowerCase();
        }
        if (isImage(ftype)) {
            var id = $(this).attr("id");
            if (obj == id) {
                res = index;
            }
            index++;
        }

    });
    return res;
}
function thisPageImage() {
    var selectId = [];
    $("#myFilesAllItem").find(".myFilesOneItem").each(function (i, e) { //i 是1、2、3
        var fname = $(this).attr("fname");
        var fid = $(this).attr("fid");
        var ftype = $(this).attr("ftype");
        if (ftype != null) {
            ftype = ftype.toLowerCase();
        }
        if (isImage(ftype)) {
            var url = '/FilePreview/SmallImage/' + fid;
            var ourl = '/FilePreview/Image/' + fid;
            var o = {
                'alt': "<a href ='" + ourl + "' target='_blank' >" + fname + "</a>",
                'pid': fid, 'src': url, 'thumb': ''
            };
            selectId.push(o);
        }
    });
    return selectId;
}

//FunctionCommon.js 文档中心功能控制

function deleteFiles(type, menuType) {
    var selectedId = getSelectFiles();
    //alert(selectedId);
    if (menuType == undefined || menuType == null) {
        menuType = "";
    }

    if (selectedId == "") {
        showError('请先选择文件');
        return;
    } else {
        layer.confirm('确定删除所选项?', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                async: true,
                url: "MyFilesHandler.ashx?m=DelFiles&t=" + new Date().getMilliseconds(),
                data: { "selectedid": selectedId, "type": type, "menuType": menuType },
                dataType: "json",
                success: function (data) {
                    var officeUrl = data;
                    if (officeUrl) {
                        //alert("操作成功");
                        showSuccess("操作成功");
                        layer.closeAll();
                        refresh();
                    }
                    else {
                        //alert(error);
                        showError('操作失败!');
                        //alert("操作失败");
                    }
                }
            });
        }, function (error) {

        });
    }
}

//重命名的方法
function renameFile(type, menuType) {
    if (menuType == undefined || menuType == null) { menuType = ""; }
    var idName = getSelectIdAndName();
    var arr = idName.split('|');
    if (arr != null && arr.length == 2) {
        var selId = arr[0];
        var selName = arr[1];
        layer.prompt({
            title: '请输入新名称',
            value: selName, //初始时的值，默认空字符
            shadeClose: false, //点击遮罩关闭
            shade: false,
            skin: 'layui-layer-lan'
        }, function (val) {
            if (val == selName) {
                layer.msg('名称无任何修改！');    //名称无任何修改
                return;
            }
            var reg = new RegExp('^[^\\\\\\/:*?\\"<>|]+$');
            if (!reg.test(val)) {
                layer.msg('文件夹名不合法，不可出现 \/:*?"<>| 符号');
                return;
            }
            if (getByteLen(val.trim()) > 40) {
                layer.msg('文件名过长，最大长度40');
                return;
            }
            $.post("MyFilesHandler.ashx?m=RenameFile&=" + new Date().getMilliseconds(),
            {
                type: type,
                selectedid: selId,
                newName: val,
                folderId: menuType
            }, function (jsonData) {
                if (jsonData.result != null) {
                    if (jsonData.result) {
                        layer.msg('重命名成功！');
                        refresh();
                    }
                    else {
                        layer.msg(jsonData.msg);
                    }
                } else {
                    layer.msg('重命名失败！');
                }
            });
        });
    }
}
function getSelectIdAndName() {
    var selectedId = "";
    var lis = $("#myFilesAllItem .myFilesOneItem");
    for (var i = 0; i < lis.length; i++) {
        if (lis[i] != null && $(lis[i]).hasClass("active")) {
            selectedId += lis[i].id + "|" + $(lis[i]).attr("filename");
            break;
        }
    }
    return selectedId;
}

function openShareLink(shareType) {
    var selectedId = getSelectFiles();
    if (selectedId == "") {
        showError('请先选择文件');
        return;
    } else {
        layer.open({
            type: 2,
            title: '链接共享',
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            fix: false,
            area: ['600px', '340px'],
            content: "ShareLinkAdd.aspx?shareType=" + shareType + "&ids=" + selectedId,
            end: function () {
            }
        });
    }
}

function filedown() {
    var fileidAndName = getDownParas();
    if (fileidAndName == "") {
        alert('请先选择文件');
        return;
    } else {
        var idName = fileidAndName.split('|');
        if (idName.length > 1) {
            var fileid = fileidAndName.split('|')[0];
            var filename = fileidAndName.split('|')[1];
            window.location.href = "FileDownLoad.aspx?fileid=" + fileid + "&filename=" + filename;
        }
    }
}

function getDownParas() {
    var fileIdName = "";
    var lis = $("#myFilesAllItem .myFilesOneItem");
    for (var i = 0; i < lis.length; i++) {
        if (lis[i] != null && $(lis[i]).hasClass("active")) {
            fileIdName += $(lis[i]).attr("fileid") + "|" + $(lis[i]).attr("filename");
        }
    }
    return fileIdName;
}

//换行...
function getByteLen(val) {
    var len = 0;
    for (var i = 0; i < val.length; i++) {
        if (val[i].match(/[^\x00-\xff]/ig) != null) //全角 
            len += 2;
        else
            len += 1;
    }
    return len;
}

function getByteVal(val, max) {
    var returnValue = '';
    var byteValLen = 0;
    for (var i = 0; i < val.length; i++) {
        if (val[i].match(/[^\x00-\xff]/ig) != null)
            byteValLen += 2;
        else
            byteValLen += 1;
        if (byteValLen > max)
            break;
        returnValue += val[i];
    }
    return returnValue;
}
//控制按钮权限
function checkBtnShow(on, off) {
    if (on != "") {
        var onList = on.split('|');
        for (var i = 0; i < onList.length; i++) {
            if (onList[i] != "") {
                $("#" + onList[i] + "").css("display", "");
            }
        }
    }
    if (off != "") {
        var offList = off.split('|');
        for (var i = 0; i < offList.length; i++) {
            if (offList[i] != "") {
                $("#" + offList[i] + "").css("display", "none");
            }
        }
    }
}
//.format方法
String.prototype.format = function (args) {
    var result = this;
    if (arguments.length > 0) {
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                if (args[key] != undefined) {
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    var reg = new RegExp("({[" + i + "]})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
}

//复选框change事件
function checkchange() {
    var check = $("#allFilesSelectCheckbox");
    var length = $("#myFilesAllItem .myFilesOneItem input[type=checkbox]").length;
    if (check != null && length > 0) {
        if ($("#myFilesAllItem .myFilesOneItem input[type=checkbox]:checked").length == length) {
            check[0].checked = true;
        } else {
            check[0].checked = false;
        }
    }
}

